package org.moore.controller;

import org.moore.model.Map;
import org.moore.model.User;
import org.moore.utils.Countdown;
import org.moore.utils.Data;
import org.moore.utils.MusicUtils;
import org.moore.utils.SaveMapUtils;
import org.moore.view.*;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

// 菜单栏监听类
public class GameMenuListener implements ActionListener {
    private final GamePanel gamePanel;
    private final GameFrame gameFrame;
    private final SaveMapPanel saveMapPanel;
    private final MusicUtils musicUtils;

    public GameMenuListener(GameFrame gameFrame, GamePanel gamePanel, SaveMapFrame saveMapFrame, SaveMapPanel saveMapPanel, MusicUtils musicUtils) {
        this.gameFrame = gameFrame;
        this.gamePanel = gamePanel;
        this.saveMapPanel = saveMapPanel;
        this.musicUtils = musicUtils;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // 创建一个新的字体对象
        Font newFont = new Font("宋体", Font.PLAIN, 18);
        // 设置所有的OptionPane字体
        UIManager.put("OptionPane.messageFont", newFont);
        UIManager.put("OptionPane.buttonFont", newFont);
        // 获取动作命令
        String action = e.getActionCommand();
        // 根据指令执行不同的代码
        switch (action) {
            case "start" :
                // 开始游戏
                Object[] startOptions = { "步数模式", "时间模式" };
                int startInput = JOptionPane.showOptionDialog(gameFrame, "请选择游戏模式", "模式选择",
                        JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, startOptions, startOptions[0]);
                // 模式选项判断
                if (startInput == 0) {
                    Map.setMapType(1);
                } else if (startInput == 1) {
                    Map.setMapType(2);
                }
                // 跳转关卡选择
                GamePanel.getCardLayout().show(gamePanel,"level");
                // 设置按钮状态
                GameFrame.getGameBar().getRestartItem().setEnabled(true);
                GameFrame.getGameBar().getStartItem().setEnabled(false);
                GameFrame.getGameBar().getHomeItem().setEnabled(true);
                resetGame();
            case "restart" :
                // 重新开始游戏
                Object[] colorOptions = { "确认", "取消" };
                int colorInput = JOptionPane.showOptionDialog(gameFrame, "即将重新开始游戏，未保存内容将丢失，是否继续？", "确认继续",
                        JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, colorOptions, colorOptions[0]);
                if (colorInput == 0) {
                    new Map (9,9,Map.getLevel());
                    resetGame();
                }
                break;
            case "exit" :
                // 退出选项
                Object[] exitOptions = { "退出", "取消" };
                int exitInput = JOptionPane.showOptionDialog(gameFrame, "您真的要退出吗？", "确认退出",
                        JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, exitOptions, exitOptions[0]);
                // 退出选项判断
                if (exitInput == 0) {
                    // 关闭窗口
                    System.exit(0);
                }
                break;
            case "road" :
                // 读档存档
                SaveMapFrame saveMapFrame = new SaveMapFrame(saveMapPanel);
                saveMapFrame.setVisible(true);
                // 从数据库读取判断是否有内容再进行展示
                if(!SaveMapUtils.roadMapText(User.getId(),0).isEmpty()){
                    saveMapPanel.getSave1().setText(SaveMapUtils.roadMapText(User.getId(),0));
                }
                if(!SaveMapUtils.roadMapText(User.getId(),1).isEmpty()){
                    saveMapPanel.getSave2().setText(SaveMapUtils.roadMapText(User.getId(),1));
                }
                if(!SaveMapUtils.roadMapText(User.getId(),2).isEmpty()){
                    saveMapPanel.getSave3().setText(SaveMapUtils.roadMapText(User.getId(),2));
                }
                if(!SaveMapUtils.roadMapText(User.getId(),3).isEmpty()){
                    saveMapPanel.getSave4().setText(SaveMapUtils.roadMapText(User.getId(),3));
                }
                if(!SaveMapUtils.roadMapText(User.getId(),4).isEmpty()){
                    saveMapPanel.getSave5().setText(SaveMapUtils.roadMapText(User.getId(),4));
                }
                if(!SaveMapUtils.roadMapText(User.getId(),5).isEmpty()){
                    saveMapPanel.getSave6().setText(SaveMapUtils.roadMapText(User.getId(),5));
                }
                break;
            case "rankingList" :
                // 排行榜
                RankingListFrame rankingListFrame = new RankingListFrame();
                rankingListFrame.setVisible(true);
                break;
            case "home" :
                // 返回主界面
                Object[] homeOptions = { "确认", "取消" };
                int homeInput = JOptionPane.showOptionDialog(gameFrame, "您要返回至主界面吗？", "回到主界面",
                        JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, homeOptions, homeOptions[0]);
                // 返回判断
                if (homeInput == 0) {
                    GamePanel.getCardLayout().show(gamePanel,"home");
                    GameFrame.getGameBar().getStartItem().setEnabled(true);
                }
                break;
            case "blockColor" :
                // 单手剑数量
                String blockColor = JOptionPane.showInputDialog(gameFrame, "请输入单手剑数量：（只能3-6，为空恢复默认）");
                try {
                    if (blockColor.isEmpty()) {
                        JOptionPane.showMessageDialog(gameFrame, "未输入，恢复默认！");
                        Data.CUSTOM = 0;
                    } else if (Integer.parseInt(blockColor) < 3 || Integer.parseInt(blockColor) > 6){
                        JOptionPane.showMessageDialog(gameFrame, "非法输入！");
                        return;
                    } else {
                        Data.CUSTOM = 1;
                        Map.setMaxBlockType(Integer.parseInt(blockColor));
                    }
                } catch (NullPointerException ex) {
                    return;
                }
                // 重新开始游戏
                Object[] resStartOptions = { "确认", "取消" };
                int resStartInput = JOptionPane.showOptionDialog(gameFrame, "即将重新开始游戏，未保存内容将丢失，是否继续？", "确认继续",
                        JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, resStartOptions, resStartOptions[0]);
                if (resStartInput == 0) {
                    new Map (9,9,Map.getLevel());
                    resetGame();
                }
                break;
            case "mapShape" :
                String[] options = {"方形", "菱形", "心形"};
                int response = JOptionPane.showOptionDialog(null, "请选择地图形状", "地图选择",
                        JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
                Object[] Options = { "确认", "取消" };
                int Input = JOptionPane.showOptionDialog(gameFrame, "即将重新开始游戏，未保存内容将丢失，是否继续？", "确认继续",
                        JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, Options, Options[0]);
                if (Input == 0) {
                    if (response == 0) {
                        new Map (9,9,1);
                        resetGame();
                    } else if (response == 1) {
                        new Map (9,9,2);
                        resetGame();
                    } else if (response == 2) {
                        new Map (9,9,3);
                        resetGame();
                    }
                }
                break;
            case "obsNum" :
                // 障碍物个数
            case "obsDisappear" :
                // 障碍物消失
            case "obsAppear" :
                // 障碍物自动出现
                // 未写障碍物逻辑，以后再说
                JOptionPane.showMessageDialog(gameFrame, "开发中，敬请期待！");
                break;
            case "musicOff" :
                // 音效关
                musicUtils.pause();
                GameFrame.getGameBar().getMusicOffItem().setEnabled(false);
                GameFrame.getGameBar().getMusicOffItem().setText("音效关[√]");
                GameFrame.getGameBar().getMusicOnItem().setEnabled(true);
                GameFrame.getGameBar().getMusicOnItem().setText("音效开");
                break;
            case "musicOn"  :
                // 音效开
                musicUtils.setLoop(true);
                musicUtils.play();
                GameFrame.getGameBar().getMusicOffItem().setEnabled(true);
                GameFrame.getGameBar().getMusicOffItem().setText("音效关");
                GameFrame.getGameBar().getMusicOnItem().setEnabled(false);
                GameFrame.getGameBar().getMusicOnItem().setText("音效开[√]");
                break;
            case "gameMode" :
                // 游戏模式
                Object[] modeOptions = { "步数模式", "时间模式" };
                int modeInput = JOptionPane.showOptionDialog(gameFrame, "请选择游戏模式", "模式选择",
                        JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, modeOptions, modeOptions[0]);
                Data.CUSTOM = 0;
                // 模式选项判断
                if (modeInput == 0) {
                    Map.setMapType(1);
                } else if (modeInput == 1) {
                    Map.setMapType(2);
                }
                // 重新开始游戏
                Object[] yesModeOptions = { "确认", "取消" };
                int yesModeInput = JOptionPane.showOptionDialog(gameFrame, "即将重新开始游戏，未保存内容将丢失，是否继续？", "确认继续",
                        JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, yesModeOptions, yesModeOptions[0]);
                if (yesModeInput == 0) {
                    new Map(9,9,Map.getLevel());
                    resetGame();
                }
                break;
            case "stepsNumber" :
                // 自定义步数
                String stepsInput = JOptionPane.showInputDialog(gameFrame, "请输入自定义步数：");
                Object[] stepsOptions = { "确认", "取消" };
                if (Integer.parseInt(stepsInput) > 60 || Integer.parseInt(stepsInput) < 10) {
                    JOptionPane.showMessageDialog(gameFrame, "非法输入！");
                    return;
                }
                int stepsModeInput = JOptionPane.showOptionDialog(gameFrame, "即将重新开始游戏，未保存内容将丢失，是否继续？", "确认继续",
                        JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, stepsOptions, stepsOptions[0]);
                if (stepsModeInput == 0) {
                    new Map(9, 9, Map.getLevel());
                    // 重置数值
                    Data.SCORE = 0;
                    if (Map.isMapType() == 1) {
                        Data.STEPS = Integer.parseInt(stepsInput);
                    } else {
                        JOptionPane.showMessageDialog(gameFrame, "非当前模式，设置失败！");
                    }
                    Data.CUSTOM = 0;
                }
                break;
            case "timeNumber" :
                // 自定义时间
                String timeInput = JOptionPane.showInputDialog(gameFrame, "请输入自定义时间：");
                if (Integer.parseInt(timeInput) > 180 || Integer.parseInt(timeInput) < 30) {
                    JOptionPane.showMessageDialog(gameFrame, "非法输入！");
                    return;
                }
                Object[] timeOptions = { "确认", "取消" };
                int timeModeInput = JOptionPane.showOptionDialog(gameFrame, "即将重新开始游戏，未保存内容将丢失，是否继续？", "确认继续",
                        JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, timeOptions, timeOptions[0]);
                if (timeModeInput == 0) {
                    new Map(9,9,Map.getLevel());
                    // 重置数值
                    Data.SCORE = 0;
                    if (Map.isMapType() == 2) {
                        try {
                            Countdown.pause();
                        } catch (Exception ex) {
                            System.out.println();
                        }
                        new Countdown(Integer.parseInt(timeInput));
                        Data.COUNT = 0;
                    } else {
                        JOptionPane.showMessageDialog(gameFrame, "非当前模式，设置失败！");
                    }
                    Data.CUSTOM = 0;
                }
                break;
            case "lower" :
                // 低级
                Object[] lowerModeOptions = { "确认", "取消" };
                int lowerModeInput = JOptionPane.showOptionDialog(gameFrame, "即将重新开始游戏，未保存内容将丢失，是否继续？", "确认继续",
                        JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, lowerModeOptions, lowerModeOptions[0]);
                if (lowerModeInput == 0) {
                    new Map(9,9,1);
                    resetGame();
                }
                // 同时设置菜单栏
                GameFrame.getGameBar().getLowerModeItem().setEnabled(false);
                GameFrame.getGameBar().getMiddenModeItem().setEnabled(true);
                GameFrame.getGameBar().getPremiumModeItem().setEnabled(true);
                GameFrame.getGameBar().getLowerModeItem().setText("初级[√]");
                GameFrame.getGameBar().getMiddenModeItem().setText("中级");
                GameFrame.getGameBar().getPremiumModeItem().setText("高级");
                break;
            case "midden" :
                // 中级
                Object[] middenModeOptions = { "确认", "取消" };
                int middenModeInput = JOptionPane.showOptionDialog(gameFrame, "即将重新开始游戏，未保存内容将丢失，是否继续？", "确认继续",
                        JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, middenModeOptions, middenModeOptions[0]);
                if (middenModeInput == 0) {
                    new Map(9,9,2);
                    resetGame();
                }
                // 同时设置菜单栏
                GameFrame.getGameBar().getLowerModeItem().setEnabled(true);
                GameFrame.getGameBar().getMiddenModeItem().setEnabled(false);
                GameFrame.getGameBar().getPremiumModeItem().setEnabled(true);
                GameFrame.getGameBar().getLowerModeItem().setText("初级");
                GameFrame.getGameBar().getMiddenModeItem().setText("中级[√]");
                GameFrame.getGameBar().getPremiumModeItem().setText("高级");
                break;
            case "premium" :
                // 中级
                Object[] premiumModeOptions = { "确认", "取消" };
                int premiumModeInput = JOptionPane.showOptionDialog(gameFrame, "即将重新开始游戏，未保存内容将丢失，是否继续？", "确认继续",
                        JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, premiumModeOptions, premiumModeOptions[0]);
                if (premiumModeInput == 0) {
                    new Map(9,9,3);
                    resetGame();
                }
                // 同时设置菜单栏
                GameFrame.getGameBar().getLowerModeItem().setEnabled(true);
                GameFrame.getGameBar().getMiddenModeItem().setEnabled(true);
                GameFrame.getGameBar().getPremiumModeItem().setEnabled(false);
                GameFrame.getGameBar().getLowerModeItem().setText("初级");
                GameFrame.getGameBar().getMiddenModeItem().setText("中级");
                GameFrame.getGameBar().getPremiumModeItem().setText("高级[√]");
                break;
            case "intro" :
                // 游戏说明
                JOptionPane.showMessageDialog(gameFrame, "游戏帮助：点击任意模式并选择关卡后\n" +
                        "即可开始游戏，点击方块与相邻方块交\n" +
                        "换，若可消除即可获得分数，步数或者\n" +
                        "时间用完即刻结束，可选择进入下一关\n" +
                        "或者返回，游戏过程中可随时存档保存，\n" +
                        "每个关卡和模式所获得的分数将记录到\n" +
                        "排行榜中，游戏还拥有多种自定义功能，\n" +
                        "祝您游戏愉快！————开发者：Moore");
                break;
            case "about" :
                // 关于游戏
                JOptionPane.showMessageDialog(gameFrame,"关于“武侠消消乐”\n" +
                        "版本号：1.0\n" +
                        "开发人员：Moore\n" +
                        "编程语言：Java8u411\n" +
                        "编写软件：IntelliJ IDEA\n" +
                        "联系方式：light2253@qq.com\n" +
                        "本软件遵循GPL-2.0协议开源\n" +
                        "开源地址：https://gitee.com/\n" +
                        "moore123/cancellation\n" +
                        "Copyright © 2024 Moore.\n" +
                        "All rights reserved.");
            case "donation" :
                // 捐赠
                DonateFrame donateFrame = new DonateFrame();
                donateFrame.setVisible(true);
        }
    }
    public static void resetGame() {
        // 重置数值
        Data.SCORE = 0;
        if (Map.isMapType() == 1) {
            Data.STEPS = 15;
        } else if (Map.isMapType() == 2) {
            try {
                Countdown.pause();
            } catch (Exception ex) {
                System.out.println();
            }
            new Countdown(40);
            Data.COUNT = 0;
        }
        Data.CUSTOM = 0;
    }
}
